/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.dao;

import net.martinimix.beans.bind.BindingService;

import com.bluemartini.database.BMDatabaseManager;
import com.bluemartini.database.DBUtil;
import com.bluemartini.dna.BMException;
import com.bluemartini.dna.BusinessObject;

/**
 * Provides a simple business object template implementation.
 * 
 * @author Scott Rossillo
 *
 */
public class SimpleBusinessObjectTemplate implements BusinessObjectTemplate {

	private final BindingService bindingService;
	private final String database;
	
	/**
	 * Creates a new simple business object template.
	 *  
	 * @param bindingService the <code>BindingService</code> this template will
	 * use to convert beans to business objects and vice versa
	 * 
	 * @param database the name of the database to which this template will save
	 * business objects
	 */
	public SimpleBusinessObjectTemplate(BindingService bindingService, String database) {
		
		this.bindingService = bindingService;
		this.database = database;
	}
	
	/* (non-Javadoc)
	 * @see net.martinimix.dao.BusinessObjectTemplate#insert(java.lang.Object, java.lang.String)
	 */
	public Object insert(Object bean, String type) {
	
		return save(bean, type, true);
	}

	/* (non-Javadoc)
	 * @see net.martinimix.dao.BusinessObjectTemplate#update(java.lang.Object, java.lang.String)
	 */
	public Object update(Object bean, String type) {
		
		return save(bean, type, false);
	}

	/* (non-Javadoc)
	 * @see net.martinimix.dao.BusinessObjectTemplate#save(java.lang.Object, java.lang.String, boolean)
	 */
	public Object save(Object bean, String type, boolean isNew) {
		
		BusinessObject boInput = bindingService.bindBusinessObject(bean, type);
		Object out;
		
		BMDatabaseManager.pushCurrentDatabase(database);
		try {
			if(isNew) {
				DBUtil.insertBusinessObject(boInput, true);
			} else {
				DBUtil.updateBusinessObject(boInput, true);
			}
			out = bindingService.bindBean(boInput, bean.getClass().newInstance());
		} catch (BMException e) {
			throw new DataAccessException("Error saving bean [" + bean + "]:" + e.getMessage(), e);
		} catch (InstantiationException e) {
			throw new IllegalArgumentException(
					"Target bean [" + bean.getClass() + "] must provide a empty public constructor");
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException(
					"Target bean [" + bean.getClass() + "] must provide a empty public constructor");
		} finally {
			BMDatabaseManager.popCurrentDatabaseQuiet(database);
		}
		
		return out;
	}
}
